'use strict';

const BaseService = require('./base');

class UserService extends BaseService {
  constructor(...props) {
    super('User' ,...props)
  }

  //根据字段查询获取
  async findByFields(where, findType = 'findOne') {
    const { primary_model, ctx } = this;
    return await primary_model[findType]({
      where,
      attributes: ['id', 'nickname', 'password', 'status']
    })
  }
  //查找，keywords 标题，副标题，人名
  async findAllByKeyWord(searchWhere) {
    const { primary_model, ctx } = this
    const { page, limit } = searchWhere
    const pageWhere = {}
    if(page && limit) {
      pageWhere.limit = limit
      pageWhere.offset = (page - 1)*limit
    }
    //文章查找条件
    const userFindWhere = {
      attributes: ['id', 'nickname', 'password', 'status'],
      ...pageWhere
    }
    const users = await primary_model.findAll(userFindWhere)
    const userCount = await primary_model.count({col: 'id'})
    return {users, userCount}
  }
}

module.exports = UserService;
